package com.hzsai.utils;

import com.hzsai.handler.ResultSetHandler;
import org.springframework.validation.ObjectError;

import java.sql.*;
import java.util.List;

public class SQLUtils {

    // uri
    private static final String uri = "jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&characterEncoding=utf-8";

    private static final String driver = "com.mysql.cj.jdbc.Driver";

    private static final String username = "root";

    private static final String password = "123456";

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Connection conn = null;
        Class.forName(driver);
        conn = DriverManager.getConnection(uri, username, password);
        return conn;
    }

    public static Connection getConnection(String username, String password) throws SQLException, ClassNotFoundException {
        Connection connection = null;
        Class.forName(driver);
        connection = DriverManager.getConnection(uri, username, password);
        return connection;
    }

    public static void update(String sql, Object[] params){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            System.out.println("执行sql: " + sql);
            for (int i = 0; i < params.length; ++i) {
                ps.setObject(i + 1, params[i]);
            }
            System.out.println(ps.toString());
            ps.executeUpdate();
            conn.commit();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("连接出错了!!!");

        } catch (SQLException e) {
            e.printStackTrace();
        }
        release(conn, ps, rs);
    }

    public static Object query(String sql, Object[] params, ResultSetHandler rsh)  {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; ++i) {
                ps.setObject(i + 1, params[i]);
            }
            rs = ps.executeQuery();
            return rsh.handler(rs);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("驱动异常");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("sql执行异常");
        }
        release(conn, ps, rs);

        return null;
    }

    public static void release(Connection conn, PreparedStatement ps, ResultSet rs) {

        if (ps != null) try {
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Object count(String sql, Object[] params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Object cnt = null;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; params != null && i < params.length; ++i) {
                // 序号是从1开始的
                ps.setObject(i+1, params[i]);
            }
            rs = ps.executeQuery();
            if (rs.next()) {
                // 序号是从1开始的
                cnt = rs.getInt(1);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cnt;
    }

    public static String getUri() {
        return uri;
    }

    public static String getDriver() {
        return driver;
    }

    public static String getUsername() {
        return username;
    }

    public static String getPassword() {
        return password;
    }
}
